package com.raja.servingStation.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.raja.servingStation.dao.ClaimDao;
import com.raja.servingStation.entity.KmAndMonth;
import com.raja.servingStation.service.ClaimService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by zhangzhen on 2017/10/10.
 */
@Service
public class ClaimServiceImpl implements ClaimService {
    @Autowired
    private ClaimDao claimDao;
    private static Integer BLAMETYPE = 11;
    private static Integer CLAIMSTATE = 2;


    @Override
    public Object claimSbOrderByOrderId(JSONObject body) throws Exception {
        return claimDao.claimSbOrderByOrderId(body);
    }

    @Override
    public Object claimRepairOrderByOrderId(JSONObject body) throws Exception {

        Integer blameType = claimDao.getBlameType(body);
        if(blameType.intValue()==BLAMETYPE.intValue()){
            return "产生的故障为用户原因，不满足索赔条件！";
        }

        //查询当前工单车辆的总里程和开票时间
        KmAndMonth carInfo = claimDao.getCarInfo(body);
        //自身的里程
        float km = carInfo.getKm();
        //从销售至今多长时间
        int month = carInfo.getMonth();

        //查询工单下的所有零件的保养日期和保养公里
        List<KmAndMonth> bxInfo = claimDao.getBxInfo(body);

        for (KmAndMonth b : bxInfo) {
            // 当前里程和时间都没有超出保修范围，才能提起索赔
            if ( (km < b.getKm()) && (month < b.getMonth()) ) {
                return "车辆当前里程和维修所用零件时长都没有超出保修范围，不满足索赔条件！";
            }
        }

        //查询当前单子是否已经发起了索赔，并且索赔流程并未被驳回
        Integer claimState = claimDao.getClaimState(body);
        if(claimState < CLAIMSTATE){
            return "已经发起了索赔，不能再次发起索赔！";
        }


        return claimDao.claimRepairOrderByOrderId(body);
    }
}
